
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.sys.model.SysMenu;
import com.example.demo.modules.sys.model.SysRole;
import com.example.demo.modules.sys.service.MenuService;
import com.example.demo.modules.sys.vo.SysMenuVo;
import com.example.demo.modules.sys.vo.SysUserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.example.demo.core.entity.ProcessResult.ERROR;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @RequestMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("/modules/sys/sysMenu/list");
    }
    @RequestMapping("/selIcons")
    public ModelAndView selIcons() {
        return new ModelAndView("/modules/sys/sysMenu/icons");
    }

    @RequestMapping("/treeList")
    public List<SysMenuVo> treeList(){
        return menuService.getTreeList();
    }

    @RequestMapping("/treeListPermission")
    public List<SysMenuVo> treeListPermission(HttpServletRequest request){
        SysUserVo sysUserVo=(SysUserVo)request.getSession().getAttribute("user");
        return menuService.treeListPermission(sysUserVo);
    }
   @RequestMapping
    public List<SysMenu> getAll(SysMenu sysMenu, String keyword) {
        List<SysMenu> sysMenuList = menuService.getAll(sysMenu,keyword);
        //return new PageResult(new PageInfo<SysMenu>(sysMenuList));
       return sysMenuList;
    }

    @RequestMapping(value = "/selectMenuById/{id}")
    public SysMenu view(@PathVariable Integer id) {
        if(id!=null){
            SysMenu sysMenu = menuService.getById(id);
            return sysMenu;
        }
        return null;
    }

    @RequestMapping(value = "/saveOrUpdate")
    public ProcessResult saveOrUpdate(SysMenu sysMenu) {
        try {
            menuService.saveOrUpdate(sysMenu);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/delete/{id}")
    public ProcessResult delete(@PathVariable Integer id) {
        try {
            menuService.deleteById(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping("/getSelMenuPermission/{roleId}")
    public List<SysMenu> getSelMenuPermission(@PathVariable Integer roleId){
        return menuService.getSelMenuPermission(roleId);
    }

    @RequestMapping("/saveMenuPermission")
    public ProcessResult saveMenuPermission(Integer roleId,@RequestParam("ids[]") Integer[] ids){
        try {
            menuService.saveMenuPermission(roleId,ids);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

}
